package psn.zjk.common.easyexcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import psn.zjk.common.domain.ExcelDomain;
import psn.zjk.common.domain.SchoolClass;
import psn.zjk.common.domain.Teacher;
import psn.zjk.common.domain.User;

import java.io.*;
import java.util.Arrays;

public class EasyExcelDemo {


    public static void export2(InputStream inputStream, OutputStream outputStream){
        ExcelDomain excelDomain = new ExcelDomain();
        excelDomain.setAge("zz");
        excelDomain.setName("zz");

        ExcelWriter writer = EasyExcel.write(outputStream).withTemplate(inputStream).build();
        WriteSheet sheet = EasyExcel.writerSheet().build();
        writer.fill(excelDomain,sheet);
        writer.finish();
    }
    public static void export(InputStream inputStream, OutputStream outputStream){
        SchoolClass schoolClass = new SchoolClass();
        User user = new User();
        user.setAge(11);
        user.setName("tom");
        user.setSex(0);
        Teacher teacher = new Teacher();
        teacher.setName("t1");
        teacher.setClassName("english");
        schoolClass.setTeachers(Arrays.asList(teacher,teacher,teacher));
        schoolClass.setUsers(Arrays.asList(user,user,user));

        FillConfig fillConfig = new FillConfig();
        fillConfig.setDirection(WriteDirectionEnum.VERTICAL);

        ExcelWriter writer = EasyExcel.write(outputStream).withTemplate(inputStream).build();
        WriteSheet sheet = EasyExcel.writerSheet().build();
        writer.fill(new FillWrapper(".",Arrays.asList(schoolClass)),sheet);
        writer.finish();
    }

    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(getFile("template.xlsx"));
        FileOutputStream fileOutputStream = new FileOutputStream(getFile("out.xlsx"));
        export(fileInputStream,fileOutputStream);
    }

    private static File getFile(String filename) throws IOException {
        File file = new File(filename);
        if(!file.exists()){
            file.createNewFile();
        }
        return  file;
    }

}
